package com.icode.dao.impl;

import com.icode.core.dto.house.RentDTO;
import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.house.MyHouse;
import com.icode.core.model.house.MyHouseStatus;
import com.icode.core.model.house.Rent;
import com.icode.core.model.user.User;
import com.icode.core.model.user.UserInfo;
import com.icode.dao.MyHouseDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-9-16
 * Time: 上午10:42
 * To change this template use File | Settings | File Templates.
 */
@Repository("myHouseDao")
public class MyHouseDaoImpl extends EntityDaoImpl implements MyHouseDao {


    @Override
    public List<MyHouse> findMyHouse(UserInfo userInfo) {
        String hql = "from " + MyHouse.class.getName() + " a where a.userInfo=:userInfo and a.exchangeHouse=:exchangeHouse";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userInfo", userInfo);
        query.setParameter("exchangeHouse", false);
        List<MyHouse> list = query.list();
        return list.isEmpty() ? null : list;
    }

    @Override
    public UserInfo findUserInfoByUser(User user) {
        String hql = "from " + UserInfo.class.getName() + " a where a.user=:user";
        Query query = currentSession().createQuery(hql);
        query.setParameter("user", user);
        List<UserInfo> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<MyHouse> findMyChangeHouse(UserInfo userInfo) {
        String hql = "from " + MyHouse.class.getName() + " a where a.userInfo=:userInfo and a.exchangeHouse=:exchangeHouse";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userInfo", userInfo);
        query.setParameter("exchangeHouse", true);
        List<MyHouse> list = query.list();
        return list.isEmpty() ? null : list;
    }

    @Override
    public List<MyHouse> findMyTransferHouses(UserInfo userInfo) {
        String hql = "from " + MyHouse.class.getName() + " a where a.userInfo=:userInfo and a.myHouseStatus=:myHouseStatus";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userInfo", userInfo);
        query.setParameter("myHouseStatus", MyHouseStatus.ASSIGNMENTED);
        List<MyHouse> list = query.list();
        return list.isEmpty() ? null : list;
    }

    @Override
    public List<MyHouse> findMyHouseisToAssignmnet() {
        String hql = "from " + MyHouse.class.getName() + " a where a.myHouseStatus=:myHouseStatus";
        Query query = currentSession().createQuery(hql);
        query.setParameter("myHouseStatus", MyHouseStatus.TOASSIGNMNET);
        List<MyHouse> list = query.list();
        return list.isEmpty() ? null : list;
    }

    @Override
    public List<MyHouse> findMyHouseByCondition(String area, String week, String themes) {
        String hql;
        if (week.equals("不限")) {
            hql = "from " + MyHouse.class.getName() + " a where a.myHouseStatus=:myHouseStatus" + themes + area;
        } else {
            hql = "from " + MyHouse.class.getName() + " a where a.myHouseStatus=:myHouseStatus and a.houseWeek.name=:week " + themes + area;
        }
        Query query = currentSession().createQuery(hql);
        query.setParameter("myHouseStatus", MyHouseStatus.TOASSIGNMNET);
        if (!week.equals("不限")) {
            query.setParameter("week", week);
        }
        return query.list();
    }

    @Override
    public List<MyHouse> findMyHouseByUsername(String username) {
        String hql = "from " + MyHouse.class.getName() + " a where a.userInfo.username=:username and a.exchangeHouse=:exchangeHouse";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        query.setParameter("exchangeHouse", false);
        List<MyHouse> list = query.list();
        return list.isEmpty() ? null : list;
    }

    @Override
    public List<MyHouse> findMyExChangeHouseByUsername(String username, MyHouseStatus myHouseStatus, boolean exchangeHouse) {
        String hql = "from " + MyHouse.class.getName() + " a where a.userInfo.username=:username and a.myHouseStatus=:myHouseStatus and a.exchangeHouse=:exchangeHouse";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        query.setParameter("myHouseStatus", myHouseStatus);
        query.setParameter("exchangeHouse", exchangeHouse);
        return query.list();
    }
}
